package cn.stylefeng.guns.modular.basic.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicOilCard;
import cn.stylefeng.guns.modular.basic.model.params.BasicOilCardParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicOilCardResult;
import cn.stylefeng.guns.modular.basic.service.BasicOilCardService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;


/**
 * 油卡管理控制器
 * @author zhucx
 * @Date 2023-01-19 11:02:10
 */
@Controller
@RequestMapping("/basicOilCard")
public class BasicOilCardController extends BaseController {

    private String PREFIX = "/basic/basicOilCard";

    @Autowired
    private BasicOilCardService basicOilCardService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author zhucx
     * @Date 2023-01-19
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/basicOilCard.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2023-01-19
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/basicOilCard_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2023-01-19
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/basicOilCard_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2023-01-19
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicOilCardParam basicOilCardParam) {
    	User user = userService.getcurrentUser();
    	BasicOilCardParam basicOilCardParam2 = new BasicOilCardParam();
    	basicOilCardParam2.setDeliverId(user.getDeliverId());
    	basicOilCardParam2.setCardNumber(basicOilCardParam.getCardNumber());
    	BasicOilCardResult check = basicOilCardService.findBySpec(basicOilCardParam2,user.getOperateId());
    	if(check != null) {
    		return ResponseData.error("卡号已存在");
    	}
    	
    	basicOilCardParam.setDeptId(user.getDeptId());
    	basicOilCardParam.setDeliverId(user.getDeliverId());
    	basicOilCardParam.setCreateUserName(user.getName());
        this.basicOilCardService.add(basicOilCardParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2023-01-19
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicOilCardParam basicOilCardParam) {
    	User user = userService.getcurrentUser();
    	BasicOilCardParam basicOilCardParam2 = new BasicOilCardParam();
    	basicOilCardParam2.setId(basicOilCardParam.getId());
    	basicOilCardParam2.setDeliverId(user.getDeliverId());
    	basicOilCardParam2.setCardNumber(basicOilCardParam.getCardNumber());
    	BasicOilCardResult check = basicOilCardService.findBySpec(basicOilCardParam2,user.getOperateId());
    	if(check != null) {
    		return ResponseData.error("卡号已存在");
    	}
    	
    	basicOilCardParam.setDeptId(user.getDeptId());
    	basicOilCardParam.setDeliverId(user.getDeliverId());
    	basicOilCardParam.setCreateUserName(user.getName());
        this.basicOilCardService.update(basicOilCardParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     * @author zhucx
     * @Date 2023-01-19
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicOilCardParam basicOilCardParam) {
        this.basicOilCardService.delete(basicOilCardParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     * @author zhucx
     * @Date 2023-01-19
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicOilCardParam basicOilCardParam) {
        BasicOilCard detail = this.basicOilCardService.getById(basicOilCardParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     * @author zhucx
     * @Date 2023-01-19
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicOilCardParam basicOilCardParam,String condition ) {
    	User user = userService.getcurrentUser();
    	basicOilCardParam.setDeliverId(user.getDeliverId());
        return this.basicOilCardService.findPageBySpec(basicOilCardParam,condition,user.getOperateId());
    }

}


